<?php
/**
 * 毛文科
 */
namespace app\admin\model\getdatas;
use app\admin\builder\Builder;
use think\facade\Db;
use think\Request;
use tp5er\Backup;

class MySubject
{
    protected $cid = '';
    protected $model_id = '';
    protected $model = [];
    protected $field = [];

    /**设置cid
     * @param null $cid
     * @return $this
     */
    public function setCid($cid=null){
        if($cid){
            $this->cid = $cid;
            $this->model_id = '';
        }
        return $this;
    }

    /**获取cid
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCid(){
        if($this->cid){
            return Db::name(getTable('column'))
                ->where('id',$this->cid)
                ->find();
        }else{
            return [];
        }
    }

    /**设置model_id
     * @param null $model_id
     * @return $this
     */
    public function setModelId($model_id=null){
        if($model_id){
            $this->model_id = $model_id;
            $this->cid = '';
        }
        return $this;
    }

    /**使用cid获取数据
     * @param $cid
     * @return $this
     */
    public function get_cid_model($cid){
        $column = Db::name(getTable('column'))
            ->where('id',$cid)
            ->find();
        if($column){
            $model = Db::name(getTable('models'))
                ->where('id',$column['model_id'])
                ->find();
            if($model){
                $model['toops'] = explode(',',$model['toops']);
                $model['show'] = explode(',',$model['show']);
                $model['right'] = explode(',',$model['right']);
                $this->model = $model;
                $field = Db::name(getTable('fields'))
                    ->where('model_id',$model['id'])
                    ->order('sort desc')
                    ->select()->toArray();
                $this->field = $field;
            }
        }
        return $this;
    }

    /**使用model_id获取数据
     * @param $model_id
     * @return $this
     */
    public function get_model_id_model($model_id){
        $model = Db::name(getTable('models'))
            ->where('id',$model_id)
            ->find();
        $model['toops'] = explode(',',$model['toops']);
        $model['show'] = explode(',',$model['show']);
        $model['right'] = explode(',',$model['right']);
        $this->model = $model;
        if($model){
            $field = Db::name(getTable('fields'))
                ->where('model_id',$model['id'])
                ->order('sort desc,id asc')
                ->select()->toArray();
            $this->field = $field;
        }
        return $this;
    }

    /**获取模型
     * @param null $field
     * @return array|mixed
     */
    public function getModel($field=null){
        if($this->cid){
            $this->get_cid_model($this->cid);
        }
        if($this->model_id){
            $this->get_model_id_model($this->model_id);
        }
        if($field&&$this->model){
            return $this->model[$field];
        }
        return $this->model;
    }

    /**获取字段
     * @param null $field
     * @return array
     */
    public function getField($field=null){
        if($this->cid){
            $this->get_cid_model($this->cid);
        }
        if($this->model_id){
            $this->get_model_id_model($this->model_id);
        }
        if($field){
            $data = [];
            foreach ($field as $value){
                $data[] = $value[$field];
            }
            return $data;
        }
        return $this->field;
    }

    /**加载form页面
     * @param string $action
     * @param null $id
     * @param bool $edit
     * @return \think\response\View
     */
    public function getForm($action='',$id=null,$edit=true,$buxianshi=[],$pid=null){
        $builder = Builder::form()
            ->setAction($action);
        $field = $this->getField();
        $table = $this->getModel('table');
        $data = null;
        if($id){
            $data = Db::name($table)
                ->where('id',$id)
                ->find();
            $builder = $builder->setProject('none','id','',$data['id']);
        }
//        echo '<pre>';
//        print_r($field);
//        exit;
        foreach ($field as $value){
            if(in_array($value['field'],$buxianshi)){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
                continue;
            }
            if(!$value['edit']){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
            }else{
                if($value['form_type']=='单行文本'){
                    if($data){
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

                if($value['form_type']=='多行文本'){
                    if($data){
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

//单文件
                if($value['form_type']=='单文件'){
                    if($data){
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //多文件
                if($value['form_type']=='多文件'){
                    if($data){
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='单图片'){
                    if($data){
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='视频'){
                    if($data){
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='多图片'){
                    if($data){
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }

                //单选框
                if($value['form_type']=='单选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //多选框
                if($value['form_type']=='多选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //下拉框
                if($value['form_type']=='下拉框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //联动单选框
                if($value['form_type']=='联动单选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动多选框
                if($value['form_type']=='联动多选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }


                if($value['form_type']=='联动下拉框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
//                    echo '<pre>';
//                    print_r($mytable);
//                    exit;
                    $array = [];
                    if($mytable['id']??null&&$mytable['id']==14){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }elseif($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->where('delete_time',0)->field('id,name')->select()->toArray();
                    }
                    if($this->model_id==2&&$mytable['id']==10){
                        $array = Db::name($mytable['table'])->where('type','<>','1')->where('status',1)->where('delete_time',0)->field('id,name')->select()->toArray();
                    }
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        if($value['field']=='pid'){
                            if($pid){
                                $builder = $builder->setProject('select','pid',$value['field_name'],$pid,$array,$value['descs'],true);
                                continue;
                            }
                        }
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }

//联动下拉组
                if($value['form_type']=='联动下拉组'){
                    $tablesid =  explode(',',$value['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                    if($tablesid[1]??null){
                        $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                    }else{
                        echo '联动下拉组最少联两表';
                    }
                    $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                    foreach ($array as $key=>$va){
                        $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                        $array[$key] = $va;
                    }
                    if($data){
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //编辑框
                if($value['form_type']=='编辑框'){
                    if($data){
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //编辑框(layui)
                if($value['form_type']=='编辑框(layui)'){
                    if($data){
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //开关
                if($value['form_type']=='开关'){
                    if($data){
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                    }
                }

                //时间
                if($value['form_type']=='时间'){
                    if($data){
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                    }
                }
                if($value['form_type']=='json数组'){
                    $array = explode(':',$value['option']);
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($data[$value['field']],true),$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                    }
                }

            }
        }
//        echo 'haha';exit;
        if($edit){
            $builder = $builder->setButton('submit','提交');
        }
        $builder = $builder->setButton('back','返回');
        return $builder->fetch();
    }

    /**
     * @param string提交路径 $action
     * @param null是否设置id $id
     * @param bool是否需要提交按钮 $edit
     * @param array隐藏字段 $buxianshi
     * @param null是否设置cid $cid
     * @param null是否设置某特定字段 $teshu_fiels
     * @param null设置特定字段默认值 $fieldvalue
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFormToCid($action='',$id=null,$edit=true,$buxianshi=[],$cid=null,$teshu_fiels=null,$fieldvalue=null){
        $builder = Builder::form()
            ->setAction($action);
        $field = $this->getField();
        $table = $this->getModel('table');
        $mycid = $this->getCid();
//        echo '<pre>';print_r($mycid);exit;
        $myedit = [];
        if($mycid){
            $myedit = explode(',',$mycid['edit']);
        }
        $data = null;
        if($id){
            $data = Db::name($table)
                ->where('id',$id)
                ->find();
            $builder = $builder->setProject('none','id','',$data['id']);
        }

        foreach ($field as $value){
            if($teshu_fiels&&$fieldvalue){
                if($value['field']==$teshu_fiels){
                    $builder = $builder->setProject('none',$teshu_fiels,'',$fieldvalue);
                    continue;
                }
            }
            if($value['field']=='cid'){
                $builder = $builder->setProject('none','cid','',$cid);
                continue;
            }
            if(in_array($value['field'],$buxianshi)){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
                continue;
            }
            if(!in_array($value['field'],$myedit)){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
            }else{
                //单行文本
                if($value['form_type']=='单行文本'){
                    if($data){
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }
                //多行文本
                if($value['form_type']=='多行文本'){
                    if($data){
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }
                //单文件
                if($value['form_type']=='单文件'){
                    if($data){
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //多文件
                if($value['form_type']=='多文件'){
                    if($data){
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //单图片
                if($value['form_type']=='单图片'){
                    if($data){
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='视频'){
                    if($data){
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //多图片
                if($value['form_type']=='多图片'){
                    if($data){
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //单选框
                if($value['form_type']=='单选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //多选框
                if($value['form_type']=='多选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //下拉框
                if($value['form_type']=='下拉框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //联动单选框
                if($value['form_type']=='联动单选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动多选框
                if($value['form_type']=='联动多选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动下拉框
                if($value['form_type']=='联动下拉框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable['id']??null&&$mytable['id']==14){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }elseif($this->model_id==2&&$mytable['id']==10){
                        $array = Db::name($mytable['table'])->where('type','单表')->where('status',1)->where('delete_time',null)->field('id,name')->select()->toArray();
                    }elseif($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->where('delete_time',null)->field('id,name')->select()->toArray();
                    }
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动下拉组
                if($value['form_type']=='联动下拉组'){
                    $tablesid =  explode(',',$value['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                    if($tablesid[1]??null){
                        $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                    }else{
                        echo '联动下拉组最少联两表';
                    }
                    $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                    foreach ($array as $key=>$va){
                        $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                        $array[$key] = $va;
                    }
                    if($data){
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }

                //编辑框
                if($value['form_type']=='编辑框'){
                    if($data){
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //编辑框(layui)
                if($value['form_type']=='编辑框(layui)'){
                    if($data){
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //开关
                if($value['form_type']=='开关'){
                    if($data){
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                    }
                }

                //时间
                if($value['form_type']=='时间'){
                    if($data){
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                    }
                }
                if($value['form_type']=='json数组'){
                    $array = explode(':',$value['option']);
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($data[$value['field']],true),$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                    }
                }

            }
        }
//        echo 'haha';exit;
        if($edit){
            $builder = $builder->setButton('submit','提交');
        }
        $builder = $builder->setButton('back','返回');
        return $builder->fetch();
    }

    public function getFormToField($action='',$id=null,$edit=true,$model_id,$buxianshi=[]){
        $builder = Builder::form()
            ->setAction($action);
        $field = $this->getField();
        $table = $this->getModel('table');
        $data = null;
        if($id){
            $data = Db::name($table)
                ->where('id',$id)
                ->find();
            $builder = $builder->setProject('none','id','',$data['id']);
        }
        foreach ($field as $value){
            if($value['field']=='model_id'){
                $builder = $builder->setProject('none','model_id',$value['field_name'],$model_id);
                continue;
            }
            if(in_array($value['field'],$buxianshi)){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
                continue;
            }
            if(!$value['edit']){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
            }else{
                if($value['form_type']=='单行文本'){
                    if($data){
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

                if($value['form_type']=='多行文本'){
                    if($data){
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

                if($value['form_type']=='开关'){
                    if($data){
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                    }
                }

                if($value['form_type']=='时间'){
                    if($data){
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                    }
                }

                if($value['form_type']=='下拉框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }

                if($value['form_type']=='联动下拉框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = Db::name($mytable['table'])->where('type','<>',1)->where('status',1)->where('delete_time',null)->field('id,name')->select()->toArray();
//                    $array = Db::name(getTable('models'))->where('type','<>',1)->where('status',1)->where('delete_time',null)->field('id,name')->select();
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动下拉组
                if($value['form_type']=='联动下拉组'){
                    $tablesid =  explode(',',$value['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                    if($tablesid[1]??null){
                        $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                    }else{
                        echo '联动下拉组最少联两表';
                    }
                    $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                    foreach ($array as $key=>$va){
                        $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                        $array[$key] = $va;
                    }
                    if($data){
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
//联动单选框
                if($value['form_type']=='联动单选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动多选框
                if($value['form_type']=='联动多选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($mytable['id']==10){
                        $array = Db::name($mytable['table'])->where('type','<>',1)->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                if($value['form_type']=='单选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }

                if($value['form_type']=='多选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //单文件
                if($value['form_type']=='单文件'){
                    if($data){
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //多文件
                if($value['form_type']=='多文件'){
                    if($data){
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='单图片'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                if($value['form_type']=='视频'){
                    if($data){
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='多图片'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //编辑框
                if($value['form_type']=='编辑框'){
                    if($data){
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //编辑框(layui)
                if($value['form_type']=='编辑框(layui)'){
                    if($data){
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='json数组'){
                    $array = explode(':',$value['option']);
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($data[$value['field']],true),$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                    }
                }
            }
        }
        if($edit){
            $builder = $builder->setButton('submit','提交');
        }
        $builder = $builder->setButton('back','返回');
        return $builder->fetch();
    }

    /**获取新的数据
     * @param $data
     * @return array
     */
    public function getData($data){
        $field = $this->getField();
        $newdata = [];
        if($data['id']??null){
            $newdata['id'] = $data['id'];
        }
        foreach ($field as $value){
            if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='编辑框(layui)'||$value['form_type']=='单图片'||$value['form_type']=='视频'||$value['form_type']=='多图片'||$value['form_type']=='单文件'||$value['form_type']=='多文件'||$value['form_type']=='联动下拉框'||$value['form_type']=='联动下拉组'){
                if(isset($data[$value['field']])&&($data[$value['field']]||$data[$value['field']]==0)){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='时间'){
//                echo $value['field'].':'.$data[$value['field']];
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = strtotime($data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = 1;
                }else{
                    $newdata[$value['field']] = 0;
                }
            }
            if($value['form_type']=='单选框'||$value['form_type']=='联动单选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = $data[$value['field']];
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='多选框'||$value['form_type']=='联动多选框'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $newdata[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $newdata[$value['field']] = $value['defaults'];
                }
            }
            if($value['form_type']=='json数组'){
                $options = explode(':',$value['option']);
                $shujuus = $data[$value['field']];
                $newshuju = [];
                foreach ($shujuus[$options[0]] as $key=>$v){
                    $newshuju[$v] = $shujuus[$options[1]][$key];
                }
                $newdata[$value['field']] = json_encode($newshuju,JSON_UNESCAPED_UNICODE);
            }
        }
        return $newdata;
    }



    /**备份
     * @param array $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    static public function BeiFen($data=[],$isTodatabases=true){
        $config = [
            'path'=>'../application/'.getProject().'/Data/'
        ];
        $db = new Backup($config);
        $files = $db->dataList();
        $zhenze = '/'.config('database.prefix').getProject().'_/';
        $newfiles = [];
        foreach ($files as $value){
            if(preg_match($zhenze,$value['name'])){
                $newfiles[] = $value;
            }
        }
        $name = getProject().'_'.time();
        foreach ($newfiles as $value){
            $db->setFile(['name'=>$name,'part'=>1])->backup($value['name'],0);
        }
        if($isTodatabases){
            $data['path'] = '../application/'.getProject().'/Data/'.$name.'-1.sql';
            $data['name'] = $data['name'];
            $data['create_time'] = time();
            Db::name(getTable('database_system'))->where('id',3)->update(['value'=>time()]);
            if(Db::name(getTable('databases'))->insert($data)){
                return $name.'.sql';
            }else{
                return false;
            }
        }
        return $name.'-1.sql';
    }

    /**导出时备份
     * @param $project
     * @return string
     * @throws \Exception
     */
    public static function mybei($project){
        $obj  = new MyBeiFen(['path'=>'../application/'.$project.'/Data','name'=>'default']);
        $array =  $obj->getlist($project);
        $list = $obj->getdir('../application/'.$project.'/Data');
//        return $list;
        foreach ($list as $value){
            if(is_file('../application/'.$project.'/Data/'.$value)){
                chmod('../application/'.$project.'/Data/'.$value,0777);
                unlink('../application/'.$project.'/Data/'.$value);
            }
        }
        $sql = $obj->backups($array);
        return $sql;
    }
    /**还原
     * @param $sql
     * @return bool
     * @throws \Exception
     */
    public static function HuanYuan($sql){
        $db = new Backup();
        $db->setFile(['name'=>'.'.$sql,'part'=>1])->import(0);
        return true;
    }
}
